// pages/home/systemAnnouncement/systemAnnouncement.js
import api from '../../../utils/api';
import util from '../../../utils/util';
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    active: 0,
    tips: '暂无数据',
    agentList: [], // 系统
    overdueList: [], // 超期
    tobeaddedList: [], // 待补录

    currentPage: 1, // 当前页码
    pageSize: 10, // 每页显示数据
    loading: true,
    remind: '',
    nodata: false, //是否没有数据
  },
  addComma(date) {
    let str = '';
    let num = '';
    let newStr = '';
    date = date + '';
    if (date.indexOf('.') != -1) {
      num = date.split('.')[0]
      str = date.split('.')[1]
    } else {
      num = date
    }
    newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    newStr = str == '' ? newStr : newStr + '.' + str;
    return newStr;
    },

  // 删除
  delete(e) {
    let that = this;
    console.log(e)
    Dialog.confirm({
        message: '确定删除吗？',
      })
      .then(() => {
        // on confirm
        api.messageDelete({
          data: {
            messageId: e.currentTarget.dataset.id
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 200) {
              wx.showToast({
                title: '删除成功',
              })
              that.clearData()
              // let agentList=[]
              // for(let i=0;i<that.data.agentList.length;i++){
              //   if( e.currentTarget.dataset.id != that.data.agentList[i].id){
              //     agentList.push(that.data.agentList[i])
              //   }
              // }
              // that.setData({
              //   agentList:agentList
              // })
            } else {
              wx.showModal({
                content: res.data.message,
                showCancel: false,
                confirmColor: '#0041C4'
              })
            }
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  // 已读
  // read(e){
  //   let that = this;
  //   api.messageSetRead({
  //     data:{
  //       messageId:e.currentTarget.dataset.id
  //     },
  //     success:function(res){
  //       console.log(res)
  //       if(res.data.code==200){
  //         wx.showToast({
  //           title: '设置成功',
  //         })
  //         that.clearData()
  //         // for(let i=0;i<that.data.agentList.length;i++){
  //         //   if( e.currentTarget.dataset.id != that.data.agentList[i].id){
  //         //     that.data.agentList[i].readFlag = 1;
  //         //     that.data.agentList[i].readFlagStr = '已读'
  //         //   }
  //         // }
  //         // that.setData({
  //         //   agentList:that.data.agentList
  //         // })
  //       }else{
  //         wx.showModal({
  //           content:res.data.message,
  //           showCancel:false,
  //           confirmColor:'#0041C4'
  //         })
  //       }
  //     }
  //   })
  // },
  //详情
  detail(e) {
    console.log(e, "详情...")
    if (e.currentTarget.dataset.type == 3031) {
      //'到款认领申请'
      wx.navigateTo({
        url: '/pages/receive/details/details?claimId=' + e.currentTarget.dataset.id,
      })
    } else if (e.currentTarget.dataset.type == 2031) {
      //'发票作废申请' 
      wx.navigateTo({
        url: '/pages/voidDetails/voidDetails?id= ' + e.currentTarget.dataset.id,
      })
    } else if (e.currentTarget.dataset.type == 2011) {
      //'发票申请' 
      wx.navigateTo({
        url: '../../invoicedetails/invoicedetails?id=' + e.currentTarget.dataset.id,
      })
    } else if (e.currentTarget.dataset.type == 3030) {
      //'到款提示' 
      wx.switchTab({
        url: '../../receive/list/list',
      })
    } else if (e.currentTarget.dataset.type == 13) {
      //'到款认领作废申请' 
      wx.navigateTo({
        url: '/pages/receive/invalidDetails/invalidDetails?abolishId=' + e.currentTarget.dataset.id,
      })
    } else {
      console.log('完结！！！')
    }


  },

  detail2(e){
    wx.navigateTo({
      url: '/pages/invoicedetails/invoicedetails?id=' + e.currentTarget.dataset.id,
    })
  },
  detail3(e){
    console.log(e,'222')
  if (e.currentTarget.dataset.type == 1) {
     //'发票申请' 
     wx.navigateTo({
      url: '../../invoicedetails/invoicedetails?id=' + e.currentTarget.dataset.id,
    })
  } else if (e.currentTarget.dataset.type == 2) {
    //'发票作废申请' 
    wx.navigateTo({
      url: '/pages/voidDetails/voidDetails?id= ' + e.currentTarget.dataset.id,
    })
  } else if (e.currentTarget.dataset.type == 3) {
    wx.navigateTo({
      url: '/pages/receive/details/details?claimId=' + e.currentTarget.dataset.id,
    })
  } else if (e.currentTarget.dataset.type == 4) {
   //'到款认领作废申请' 
   wx.navigateTo({
    url: '/pages/receive/invalidDetails/invalidDetails?abolishId=' + e.currentTarget.dataset.id,
  })
  } else {
    console.log('detail311111')
  }},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 系统消息
  getData() {
    let that = this;
    // let unreadOnly;
    // if (that.data.active == 0) {
    //   unreadOnly = false;
    // } else {
    //   unreadOnly = true;
    // }
    api.getMyMessage({
      data: {
        searchStr: that.data.searchValue ? that.data.searchValue : '',
        pageNum: that.data.currentPage,
        pageSize: that.data.pageSize,
        // unreadOnly: unreadOnly
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          console.log(res.data.data.totalPage, "111111")
          console.log(res.data.data.pageNum, "222")

          if (res.data.data.list.length > 0) {
            console.log(res.data.data.list, "系统消息")
            for (let i = 0; i < res.data.data.list.length; i++) {
              let now = new Date(res.data.data.list[i].sendTime);
              res.data.data.list[i].sendTime = util.formatTime(now).split(' ')[0];
            }
            that.data.totalPage = res.data.data.totalPage
            that.setData({
              agentList: that.data.agentList.concat(res.data.data.list),
              loading: false,
              nodata: false,
              remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多 ' : '已经到底啦～'
            })
          } else {
            that.setData({
              nodata: true
            })
          }

        }
      }
    })
  },
  // 超期发票
  getData2() {
    let that = this;
    api.getOverdueInvoice({
      data: {
        searchStr: that.data.searchValue ? that.data.searchValue : '',
        pageNum: that.data.currentPage,
        pageSize: that.data.pageSize,
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          if (res.data.data.list.length > 0) {
            console.log(res.data.data.list, "超期发票")
            for (let i = 0; i < res.data.data.list.length; i++) {
              let now = new Date(res.data.data.list[i].time);
              res.data.data.list[i].time = util.formatTime(now).split(' ')[0];
              res.data.data.list[i].money = that.addComma(res.data.data.list[i].money)
            }
            that.data.totalPage = res.data.data.totalPage
            that.setData({
              overdueList: that.data.overdueList.concat(res.data.data.list),
              loading: false,
              nodata: false,
              remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
            })
          } else {
            that.setData({
              nodata: true
            })
          }

        }
      }
    })
  },
  // 待补录发票
  getData3() {
    let that = this;
    api.getUnfilledList({
      data: {
        searchStr: that.data.searchValue ? that.data.searchValue : '',
        pageNum: that.data.currentPage,
        pageSize: that.data.pageSize,
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          if (res.data.data.list.length > 0) {
            console.log(res.data.data.list, "待补录发票")
            for (let i = 0; i < res.data.data.list.length; i++) {
              let now = new Date(res.data.data.list[i].time);
              res.data.data.list[i].time = util.formatTime(now).split(' ')[0];
              res.data.data.list[i].money = that.addComma(res.data.data.list[i].money)
            }
            that.data.totalPage = res.data.data.totalPage
            that.setData({
              tobeaddedList: that.data.tobeaddedList.concat(res.data.data.list),
              loading: false,
              nodata: false,
              remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
            })
          } else {
            that.setData({
              nodata: true
            })
          }

        }
      }
    })
  },

  onChange(event) {
    console.log(event, '222')
    this.setData({
      active: event.detail.index,
      searchValue: '',
    })

    this.clearData()
  },
  // 清空数据并重新加载数据
  clearData() {
    this.setData({
      agentList: [],
      overdueList: [],
      tobeaddedList: [],

      loading: true,
      currentPage: 1
    })
    this.getData()
    this.getData2()
    this.getData3()
  },
  // 搜索
  onSearch(event) {

    console.log(typeof (event.detail))
    this.setData({
      searchValue: event.detail.value ? event.detail.value : event.detail,
      loading: true,
    })
    this.clearData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clearData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage > this.data.currentPage) {
      ++this.data.currentPage;
      this.setData({
        loading: true,
      })
      this.getData()
      this.getData2()
      this.getData3()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})